package offerv1;

import java.util.HashSet;
import java.util.Set;

public class Offer48 {
    //双指针
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }

        int ans = 0, n = s.length();
        int startIndex = 0, endIndex = 0;
        Set<Character> dict = new HashSet<>();
        while (endIndex < n) {
            char c = s.charAt(endIndex);
            while (dict.contains(c)) {
                dict.remove(s.charAt(startIndex));
                startIndex++;
            }
            dict.add(c);

            ans = Math.max(ans, endIndex - startIndex + 1);
            endIndex++;
        }
        return ans;
    }
}
